<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/2/6
 * Time: 14:39
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Giftbagstore extends Base
{
    /**
     * 礼包订单首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $bs_phone = trim(input('bs_phone'), ' '); // 手机号
            $bs_status = input('bs_status');
            $user_real_name = input('user_real_name');

            $where = [];
            if(isset($user_real_name) && $user_real_name != ''){
                $where['user_real_name'] = ['like', '%' . $user_real_name . '%'];
            }
            if(isset($bs_phone) && $bs_phone != ''){
                $where['bs_phone'] = ['like', '%' . $bs_phone . '%'];
            }
            if(isset($bs_status) && $bs_status != ''){
                $where['bs_status'] = $bs_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;

            $count = Db::table('up_bo_giftbag_store')
                ->join('up_u_user', 'bs_user_id = user_id', 'LEFT')
                ->where($where)
                ->count();
            $lists = Db::table('up_bo_giftbag_store')
                ->join('up_u_user', 'bs_user_id = user_id', 'LEFT')
                ->field('bs_id, user_real_name, bs_status, bs_phone, bs_yingye, bs_mentou, bs_address, bs_vip_qrcode, 
                bs_time, bs_refuse_reason')
                ->where($where)
                ->order('bs_status asc, bs_time desc')
                ->page($Nowpage,$limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 审核通过
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function bsAllow(){
        $bs_id = input('id');
        $bs_code = 'G' . str_pad($bs_id, 7, 0, STR_PAD_LEFT);
        $vip_qrcode = $this->getVipQrcode($bs_code);
        $res = Db::table('up_bo_giftbag_store')
            ->where('bs_id', $bs_id)
            ->update([
                'bs_status' => 2,
                'bs_vip_code' => $bs_code,
                'bs_vip_qrcode' => $vip_qrcode,
                'bs_check_time' => date('Y-m-d H:i:s')
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 审核拒绝
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function bsRefuse(){
        $bs_id = input('bs_id');
        if(Request::instance()->isPost()){
            $refuse_reason = input('refuse_reason');
            $res = Db::table('up_bo_giftbag_store')
                ->where('bs_id', $bs_id)
                ->update([
                    'bs_status' => 3,
                    'bs_refuse_reason' => $refuse_reason,
                    'bs_check_time' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        return $this->fetch('', ['bs_id' => $bs_id]);
    }
}